/*
 * Copyright (c) 2018-2999 深圳市阿毛科技有限公司 All rights reserved.
 *
 * https://www.lamyz.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.lam.mall.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lam.common.base.domain.Result;
import com.lam.common.context.TokenUtils;
import com.lam.framework.operLog.annotation.Log;
import com.lam.mall.app.param.UserInfoParam;
import com.lam.mall.app.vo.UserVo;
import com.lam.mall.entity.MallUser;
import com.lam.mall.service.MallUserService;

import cn.hutool.core.bean.BeanUtil;
import lombok.AllArgsConstructor;
/**
 */
@RestController
@RequestMapping("/${frontPath}/user")
@Log(title = "用户接口")
@AllArgsConstructor
public class UserApiController {

	@Autowired
	private MallUserService mallUserService;

	
	/**
	 * 查看用户接口
	 */
	@GetMapping("/userInfo")
	@Log(oper = "查看用户信息")
	public Result userInfo() {
		String userId = TokenUtils.getLoginUserid();
		MallUser user = mallUserService.getCahceByUserId(userId);
		UserVo userVo = BeanUtil.copyProperties(user, UserVo.class);
		return Result.ok(userVo);
	}

	@PutMapping("/setUserInfo")
	@Log(oper = "设置用户信息")
	public Result setUserInfo(@RequestBody UserInfoParam userInfoParam) {
		String userId = TokenUtils.getLoginUserid();
		MallUser user = new MallUser();
		user.setId(userId);
		user.setAvatar(userInfoParam.getAvatarUrl());
		user.setNickName(userInfoParam.getNickName());
		mallUserService.update(user);
		return Result.ok();
	}
}
